using System;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class Stat
{
    [SerializeField] public float baseValue;
    [SerializeField] private List<StatModifier> statModifiers = new();
    public float finalValue;
    private bool needToCalculate = true;
    public float GetValue()
    {
        if (needToCalculate)
        {
            finalValue = CalculateValue();
            needToCalculate = false;
        }
        return finalValue;
    }

    public void AddModifier(string name, float value)
    {
        StatModifier statModifier = new(name, value);
        statModifiers.Add(statModifier);
        needToCalculate = true;
    }

    public void RemoveModifier(string name)
    {
        statModifiers.RemoveAll(modifier => modifier.name == name);
        needToCalculate = true;
    }

    private float CalculateValue()
    {
        finalValue = baseValue;
        foreach (var statModifier in statModifiers)
        {
            finalValue += statModifier.value;
        }
        return finalValue;
    }
    public void SetBaseValue(float value)
    {
        baseValue = value;
        needToCalculate = true;
    }
}
[Serializable] public class StatModifier
{
    public string name;
    public float value;
    public StatModifier(string name, float value)
    {
        this.name = name;
        this.value = value;    
    }
}
